<?php
/**
* Datatype của module Dự án
* Các datatype:
*   - Collection của dự án
*   - Dự án
* 
* @author trungdt
* @since June 22, 2011
*/

class duan_duan {
    // generate form for real estate project
	function form($object, $config, $loc) {
		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();
        global $db;
        
		$form = new form();
		if (!isset($object->id)) {
            $object->name="";
            $object->col_id="";
            $object->province_id=-1;
            $object->province_ward_id=-1;
            $object->tongquan="";
            $object->hatang_dichvu="";
            // default to Gia Lai, VN :)
            $object->vitri_lat="14.058324";
            $object->vitri_long="108.277199";
            $object->vitri_zoom=5;
            $object->vitri_chitiet="";
            $object->ttthem_sodomatbang="";
            $object->ttthem_maunha="";
            $object->ttthem_thanhtoan="";
            $object->big_image=-1;
            $object->thumbnail=-1;
            $object->featured=1;
            $object->pstate=0;
			$object->seo_des='';
            
		} else {
			$form->meta('id',$object->id);
		}
        
        $form->register("name","Tên dự án",new textcontrol($object->name,50,false,250,"",true));
        
        //
        $temp=serialize($loc);
        $cols=$db->selectDropdown("duan_collections","name"," location_data = '{$temp}'");
        $form->register('col_id',"Nhóm dự án",new dropdowncontrol($object->col_id, $cols));
        $form->register('seo_des',"Giải thích ngắn gọn",new texteditorcontrol($object->seo_des,4,79,true));
        // for province and ward select
        $form->register("duan","",new rs_provinces($object->province_id, $object->province_ward_id));
        //
        $form->register("tongquan","Tổng quan",new htmleditorcontrol($object->tongquan));
        $form->register("hatang_dichvu","Hạ tầng dịch vụ",new htmleditorcontrol($object->hatang_dichvu));
        //
        $form->register("vitri_chitiet","Vị trí - chi tiết",new htmleditorcontrol($object->vitri_chitiet));
        $form->register("duan_","Bấm vào chọn tọa độ của dự án",new rs_mapselect($object->vitri_long, $object->vitri_lat, $object->vitri_zoom, $config->map_key));
        // map control to select lat & long
         $form->register("ttthem_sodomatbang","Sơ đồ mặt bằng",new htmleditorcontrol($object->ttthem_sodomatbang));
         $form->register("ttthem_maunha","Mẫu nhà",new htmleditorcontrol($object->ttthem_maunha));
         $form->register("ttthem_thanhtoan","Phương thứcthanh toán",new htmleditorcontrol($object->ttthem_thanhtoan));
        
        $form->register('thumbnail','Thumbnail', new ajax_select_file("thumbnail",$config->image_col,$object->thumbnail,true));
        $form->register('big_image','Quảng cáo dự án', new ajax_select_file("big_image",$config->image_col,$object->big_image,false));
        
        $form->register(null,'', new htmlcontrol("Chọn hình ảnh Quảng cáo dự án nếu bạn muốn có hình quảng cáo to, chi tiết, cho dự án"));
        $form->register('featured','Dự án nổi bật',new checkboxcontrol($object->featured));
        $form->register('pstate','Cho phép public dự án',new checkboxcontrol($object->pstate));
        
		$form->register('submit','',new buttongroupcontrol('Hoàn tất','','Hủy bỏ'));
		return $form;
	}

	function update($values,$object, $loc) {
        $provine=duanmodule::_GetProvince_byID(intval($values['duan_province']));
        $ward=duanmodule::_GetWard_byID(intval($values['duan_ward']),$provine->id);
        
        $object->name=_ab_safe_html_string($values['name']);
        $object->seo_des=_ab_safe_html_string($values['seo_des']);
        $object->col_id=intval($values['col_id']);
        
        $object->province_id=$provine->id;
        $object->province_ward_id=$ward->id;
        
        $object->tongquan=$values['tongquan'];
        $object->hatang_dichvu=$values['hatang_dichvu'];
        // default to Gia Lai, VN :)
        $object->vitri_lat=$values['duan_lat'];
        $object->vitri_long=$values['duan_long'];
        $object->vitri_zoom=$values['duan_zoom'];
        $object->vitri_chitiet=$values['vitri_chitiet'];
        $object->ttthem_sodomatbang=$values['ttthem_sodomatbang'];
        $object->ttthem_maunha=$values['ttthem_maunha'];
        $object->ttthem_thanhtoan=$values['ttthem_thanhtoan'];
        $object->big_image=intval($values['big_image']);
        $object->thumbnail=intval($values['thumbnail']);
        if (isset($values['pstate']))
        {
            $object->pstate=1;
        }
        else
        {
            $object->pstate=0;
        }
        if (isset($values['featured']))
        {
            $object->featured=1;
        }
        else
        {
            $object->featured=0;
        }
        if (!isset($object->id))
        {
            // if this is first create
            $object->created_on=time();
            $temp=$object->name."_".$provine->short_name;
            if ($ward->id > 0)
            {
                $temp.="_".$ward->shortname;
            }
            $temp_name=_to_vietnamese_with_nomark(_ab_safe_html_string($temp));
            $temp_name2=$temp_name;
            global $db;
            while (0<1){
                
                $temp=$db->selectObject("duan"," shortname = '{$temp_name2}'");
                if ($temp)
                {
                    $temp_name2=$temp_name."_".rand(0,999);
                }
                else
                {
                    break;
                }
            }
            $object->shortname=$temp_name2;
        }
		return $object;
	}
    
    /**
    * Build up form object for real estate project collections
    * 
    * @return form
    */
    function collections_form($object, $config=null) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        
        global $db;
        
        $form = new form();
            
        
        if (!isset($object->id)) {
            $object->name = '';
            $object->shortname='';
            $object->rank=0;
        } else {
            $form->meta('id',$object->id);
        }
        
        $form->validationScript="";
        $form->register('name','Name',new textcontrol($object->name,50,false,200));
        $form->register('shortname','Shortname',new textcontrol($object->shortname,50,false,200));
        $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
        return $form;
    }
    
    /**
    * This function update all info in real estate collections
    * @author: trungdt
    */
    function collections_update($values,$object) {
        $object->name = _ab_safe_html_string($values['name']);
        $object->shortname = _to_vietnamese_with_nomark(_ab_safe_html_string($values['shortname']));
        
        return $object;
    }
    
}

?>
